from lxml import etree
import requests #导入库
urls = ['https://movie.douban.com/']
session = requests.session()
with session:
    for url in urls:
        response = session.get(url, headers={
            'User-agent': "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.75 Safari/537.36"
        })

        content = response.text #确定格式
        #利用网站检查寻找所需要的数据 复制和对比一下xpath代码
        # XPath //div[@class='billboard-bd']//tr//td/a/text()
        html = etree.HTML(content)
        titles = html.xpath("//div[@class='billboard-bd']//tr//td/a/text()")
        for t in titles:
            print(t)
    # 爬取数据保存到文件
    fileOb = open('DATAppy.txt', 'w', encoding='utf-8')  # 打开一个文件，没有就新建一个
    fileOb.write(t)
    fileOb.close()